package com.gxa.service.impl;

import com.gxa.common.R;
import com.gxa.common.StatusComment;
import com.gxa.mapper.OutListMapper;
import com.gxa.pojo.Goods_Save;
import com.gxa.pojo.Outin;
import com.gxa.pojo.Receipt_Outin;
import com.gxa.pojo.Transfer;
import com.gxa.service.OutListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OutListServiceImpl implements OutListService {
    R r = new R();
    @Autowired(required = false)
    private OutListMapper outListMapper;
    @Override
    public R saveOutList(Receipt_Outin receipt_outin) {
        Goods_Save goods_save = null;
        Integer goods_number = null;
        boolean flag = true;
        for (Outin outin : receipt_outin.getList()) {
            goods_save = outListMapper.queryGoodsNumber(outin.getGoods_Id(),outin.getWarehouse_Id());
            goods_number = outin.getGoods_Number();
            if(goods_number > goods_save.getGoods_Save_Num()){
                flag = false;
                break;
            }
        }
        if(flag){
            List<Outin> list = receipt_outin.getList();
            int result = outListMapper.saveOutList(list);
            int result2 = outListMapper.saveReceiptOutin(receipt_outin);
            if(result > 0 && result2 > 0){
                r.setCode(200);
                r.setMsg("success");
            }
        }else {
            r.setCode(402);
            r.setMsg("当前调用仓库库存不足，请重新选择");
        }
        return r;
    }

    @Override
    public R delOutList(String receipt_outin_id) {
        int result = outListMapper.delOutList(receipt_outin_id,StatusComment.DEL_STATUS);
        if(result > 0){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
            r.setCode(500);
            r.setMsg("系统出错，请联系管理员");
        }
        return r;
    }

    @Override
    public R saveWarehouseReceipt(Receipt_Outin receipt_outin) {
        List<Outin> list = receipt_outin.getList();
        int result = outListMapper.saveOutList2(list);
        int result2 = outListMapper.saveReceiptOutin2(receipt_outin);
        if(result > 0 && result2 > 0){
            r.setCode(200);
            r.setMsg("success");
        }
        return r;
    }

    @Override
    public R queryWarehouseReceipt(Map map) {
        int currentPage = Integer.parseInt((String) map.get("currentPage"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));
        Integer newCurrentPage = (currentPage - 1) * pageSize;
        List<Map> outinList =  outListMapper.queryWarehouseReceipt(newCurrentPage,pageSize,map, StatusComment.STATUS);
        if(outinList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(outinList);
            return r;
        }else {
            r.setCode(405);
            r.setMsg("系统出错了，请联系管理员");
            return r;
        }
    }

    @Override
    public R queryOutinInfo(Map map) {
        int currentPage = Integer.parseInt((String) map.get("currentPage"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));
        Integer newCurrentPage = (currentPage - 1) * pageSize;
        List<Map> outinList =  outListMapper.queryOutinInfo(newCurrentPage,pageSize,map, StatusComment.STATUS);
        if(outinList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(outinList);
            return r;
        }else {
            r.setCode(405);
            r.setMsg("系统出错了，请联系管理员");
            return r;
        }
    }
}
